<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表
class Users_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'users';
        $this->users_info = 'users_info';
        $this->users_spread = 'users_spread';
        $this->bbs_account = 'bbs_account';
        $this->bbs_user = 'bbs_user';
        $this->bbs_count = 'bbs_count';
        $this->users_log = 'users_log';
        $this->users_sign = 'users_sign';
        $this->users_login_log = 'users_login_log';
        $this->users_qq_bind = 'users_qq_bind';
        $this->users_weibo_bind = 'users_weibo_bind';
        $this->users_weixin_bind = 'users_wx_bind';
        $this->users_alter = 'users_alter';
    }
    //获取用户社区信息
    function get_users($user_id){
        
        $this->db->select($this->bbs_user.'.*,'.$this->users_info.'.*,'.$this->bbs_account.'.*,'.$this->table.".*");
        $this->db->from($this->bbs_user);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->bbs_user.'.user_id', 'left');
        $this->db->join($this->bbs_account, $this->bbs_account.'.user_id = '.$this->bbs_user.'.user_id', 'left');
        $this->db->join($this->table, $this->bbs_user.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->bbs_user.'.user_id',$user_id);
        return $this->db->get()->row_array();
    }
    
    function get($uid){
        $this->db->select($this->table.'.*,'.$this->users_info.'.*,'.$this->bbs_account.'.*');
        $this->db->from($this->table);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->bbs_account, $this->bbs_account.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->table.'.user_id',$uid);
        return $this->db->get()->row_array();
    }
    //创建社区用户表
    function insert_bbs_user($data){
        $this->db->insert($this->bbs_user, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //创建社区账户表
    function insert_bbs_account($data){
        $this->db->insert($this->bbs_account, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //创建社区用户个人统计表
    function insert_bbs_count($data){
        $this->db->insert($this->bbs_count, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //验证用户是否存在
    function get_user_flag($where){
        return $this->db->where($where)->get($this->table)->num_rows();
    }
    //通过ID验证用户是否存在
    function get_user_flag_by_uid($uid=0){
        return $this->db->where('user_id',$uid)->get($this->table)->num_rows();
    }
    //通过用户名验证是否存在用户
    function get_user_flag_by_username($username){
        return $this->db->where('username',$username)->get($this->table)->num_rows();
    }
    //用过email验证用户是否存在
    function get_user_flag_by_email($email){
        return $this->db->where('email',$email)->get($this->table)->num_rows();
    }
    //通过手机号验证用户是否存在
    function get_user_flag_by_mobile($mobile){
        return $this->db->where('mobile',$mobile)->get($this->table)->num_rows();
    }
    
    
    function bbs_user($user_id){
        $this->db->select("*")->from($this->bbs_account)->where('user_id',$user_id);
        return $this->db->get()->num_rows();
    }
    function reg_bbs($data){
        $this->db->insert($this->bbs_account,$data);
        $this->db->insert($this->bbs_user,$data);
        $this->db->insert($this->bbs_count,$data);
    }
    
    function get_user($where){
        return $this->db->where($where)->get($this->table)->row_array();
    }
    
    function get_user_by_uid($uid=0){
        return $this->db->where('user_id',$uid)->get($this->table)->row_array();
    }
    
    function get_user_by_username($username){
        return $this->db->where('username',$username)->get($this->table)->row_array();
    }
    
    function get_user_by_mobile($mobile){
        return $this->db->where('mobile',$mobile)->get($this->table)->row_array();
    }
    
    function get_user_by_email($email){
        return $this->db->where('email',$email)->get($this->table)->row_array();
    }
    
    function insert($data){
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function update($data){
        return $this->db->where('user_id',$this->session->userdata('id'))->update($this->table, $data);
    }
    
    function user_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->table, $data);
    }
    
    function user_info_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->users_info, $data);
    }
    
    function insert_user_info($data)
    {
        $this->db->insert($this->users_info, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function insert_login_log($data){
        $this->db->insert($this->users_login_log,$data);
    }
    function insert_log($data)
    {
        $this->db->insert($this->users_log, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    /**qq快捷绑定相关start*/
    function get_qq_bind($where=array()){
        return $this->db->where($where)->get($this->users_qq_bind)->row_array();
    }
    function insert_qq_bind($data){
        $this->db->insert($this->users_qq_bind, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function del_qq_bind($user_id){
        $this->db->where("user_id",$user_id);
        $this->db->delete($this->users_qq_bind);
        return $this->db->affected_rows();
    }
    /**qq快捷绑定相关end*/
    /**Weibo快捷绑定相关start*/
    function get_weibo_bind($where=array()){
        return $this->db->where($where)->get($this->users_weibo_bind)->row_array();
    }
    function insert_weibo_bind($data){
        $this->db->insert($this->users_weibo_bind, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function del_weibo_bind($user_id){
        $this->db->where("user_id",$user_id);
        $this->db->delete($this->users_weibo_bind);
        return $this->db->affected_rows();
    }
    /**Weibo快捷绑定相关end*/
    /**微信快捷绑定相关start*/
    function get_weixin_bind($where=array()){
        return $this->db->where($where)->get($this->users_weixin_bind)->row_array();
    }
    function insert_weixin_bind($data){
        $this->db->insert($this->users_weixin_bind, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function del_weixin_bind($user_id){
        $this->db->where("user_id",$user_id);
        $this->db->delete($this->users_weixin_bind);
        return $this->db->affected_rows();
    }
    /**Weibo快捷绑定相关end*/
    
    function get_user_sign($where){
        return $this->db->select('*')->from($this->users_sign)->where($where)->order_by('create_time desc')->get()->result_array();
    }
    
    function insert_user_sign($data){
        $this->db->insert($this->users_sign, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //用户信息修改标记
    function users_alter($user_id,$type){
        if(!$type){
            $type="isupdate";
        }
        $this->db->select("*");
        $this->db->from($this->users_alter);
        $this->db->where(array("user_id"=>$user_id));
        if($this->db->get()->num_rows()==0){
            $this->db->insert($this->users_alter,array("user_id"=>$user_id,$type=>1));
        }else{
            $this->db->where('user_id',$user_id)->update($this->users_alter,array($type=>1));
        }
    }
}
